package com.company.algo.DichotomousSearch.base;

/**
 * 二分查找：
 * 1. 注意区间：
 * 如果是right=nums.length-1,则while(left<=right), 对应左闭右闭区间，left = mid+1, right = mid - 1
 * 如果是right=nums.length,则while(left<right),对应左闭又开区间，left=mid+1,right=mid
 */
public class FindTargetNum {

    public int binarySearch(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (target == nums[mid]) return mid;
            else if (target > nums[mid]) left = mid + 1;
            else if (target < nums[mid]) right = mid;
        }
        return -1;
    }

    int binarySearch2(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) return mid;
            else if (nums[mid] > target) right = mid - 1;
            else if (nums[mid] < target) left = mid + 1;
        }
        return -1;
    }

    //寻找左侧边界的二分搜索
    int left_bound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                //todo 继续缩短查找区间，向左侧缩减
                right = mid;
            } else if (nums[mid] > target) {
                right = mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            }
        }
        // target 比所有数都大
        if (left == nums.length) return -1;
        // 类似之前算法的处理方式
        return nums[left] == target ? left : -1;
    }

    //寻找右侧边界的二分搜索
    int right_bound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) left = mid + 1;
            else if (nums[mid] > target) right = mid;
            else if (nums[mid] < target) left = mid + 1;
        }
        // target 比所有数都小
        if (left == 0) return -1;
        // 类似之前算法的处理方式
        return nums[left - 1] == target ? (left - 1) : -1;
    }

    int left_bound2(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) right = mid - 1;
            else if (nums[mid] > target) right = mid - 1;
            else if (nums[mid] < target) left = mid + 1;
        }
//        if (left >= nums.length || nums[left] != target)
//            return -1;
        return left;
    }

    int right_bound2(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) left = mid + 1;
            else if (nums[mid] < target) left = mid + 1;
            else if (nums[mid] > target) right = mid - 1;
        }
        if (right < 0 || nums[right] != target)
            return -1;
        return right;
    }

    public static void main(String[] args) {
        FindTargetNum Main = new FindTargetNum();
        int[] nums = {1, 2, 2, 2, 2, 3, 6};
        int target = 4;
//        System.out.println(Main.binarySearch2(nums, target));
        System.out.println(Main.left_bound2(nums, target));
//        System.out.println(Main.right_bound2(nums, target));
    }
}
